﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace UFSSWebApp
{
    public partial class viewmessages : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        private ArrayList messlist = new ArrayList();
        private UFSSservicereference.UFSSMessage[] message;
        private int messageCount;

        protected void Page_Load(object sender, EventArgs e)
        {
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            
            message = ussc.getMessages(uc);
            messageCount = message.Length;

            for (int i = 0; i < messageCount; i++)
                messlist.Add(message[i]);

            if (!IsPostBack)
                refresh();
        }

        private void refresh()
        {
            lstMessage.Items.Clear();
            messlist.Clear();
            message = ussc.getMessages(uc);
            messageCount = message.Length;

            for (int i = 0; i < messageCount; i++)
                messlist.Add(message[i]);

            foreach (UFSSservicereference.UFSSMessage mess in messlist)
                lstMessage.Items.Add(mess.subject);

            lblError.Text = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int temp = 0;

                foreach (UFSSservicereference.UFSSMessage mess in messlist)
                {
                    if (lstMessage.SelectedIndex == temp)
                    {
                        if (ussc.delMessage(uc, mess.messageID))
                            lblError.Text = "Message Deleted.";
                        else
                            lblError.Text = "Error deleting message.";
                    }

                    temp++;
                }
                refresh();
            }
            catch
            {
                lblError.Text = "Error deleting message.";
            }
        }

        protected void lstMessage_SelectedIndexChanged(object sender, EventArgs e)
        {
            message = ussc.getMessages(uc);
            messlist.Add(message[lstMessage.SelectedIndex]);
            foreach (UFSSservicereference.UFSSMessage mess in messlist)
            {
                txtFrom.Text = mess.senderName;
                txtSubject.Text = mess.subject;
                txtMessage.Text = mess.messageCont;
                txtDateSent.Text = mess.dateAdded.ToString();
            }
        }

        protected void btnReply_Click(object sender, EventArgs e)
        {
            Session.Remove("reply");
            Session.Remove("reciever");
            if (lstMessage.SelectedIndex != -1)
            {
                Session.Add("reply", txtSubject.Text);
                Session.Add("reciever", txtFrom.Text);
                Response.Redirect("~/message.aspx");
            }
        }
    }
}